import { createStore } from 'vuex'
import websocket from './websocket'
export default createStore({

  state: {
    user: null // 存储用户信息
  },
  getters: {
    // 获取用户信息
    getUser: (state) => state.user,
    // 判断用户是否登录
    isLoggedIn: (state) => !!state.user
  },
  mutations: {
    // 设置用户信息
    SET_USER(state, user)
    {
      state.user = user
    },
    // 清除用户信息
    CLEAR_USER(state)
    {
      state.user = null
    },
    // 设置进度条状态
    SET_PROGRESS_BAR(state, payload)
    {
      state.progressBar = { ...state.progressBar, ...payload }
    }
  },
  actions: {
    // 登录成功后设置用户信息
    login({ commit }, user)
    {
      commit('SET_USER', user)
    },
    // 登出时清除用户信息
    logout({ commit })
    {
      commit('CLEAR_USER')
    }
  },
  modules: {
    websocket
  }
})
